package com.example.administrator.wallet.view.activity.mine.Setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.mylibrary.src.main.java.com.quincysx.crypto.utils.Base58;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static com.example.administrator.wallet.R.id.et_password;
import static com.loopj.android.http.AsyncHttpClient.log;

public class ModifyPasswordActivity extends AppCompatActivity {

    private ImageButton mIb_back,mIb_look,mIb_delete;
    private TextView mV_title;
    private Button mBt_next;
    private EditText mEt_password;
    private boolean isHideFirst = true;// 输入框密码是否是隐藏的，默认为true
    private CharSequence temp;
    private String datas = "";
    private String datas2 = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        setStatuColor();

        mIb_back = (ImageButton) findViewById(R.id.finish_img);
        mV_title = (TextView) findViewById(R.id.view_title);
        mBt_next = (Button) findViewById(R.id.bt_next);
        mEt_password = (EditText) findViewById(et_password);
        mIb_look = (ImageButton) findViewById(R.id.ib_look);
        mIb_delete = (ImageButton) findViewById(R.id.ib_delete);


        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.changePwd);
        mEt_password.setHint(R.string.Please_enter_the_original_password);

        //将旧密码提取出
        datas = SharedPrefsUtil.getValue(ModifyPasswordActivity.this, "wallet", "log", "");
//        log.e("aaa",datas);

        mEt_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(temp.length() == 0) {
                    mIb_delete.setVisibility(View.GONE);
                    mBt_next.setEnabled(false);
                    mBt_next.setBackgroundDrawable(getResources().getDrawable(R.drawable.backups_button_normal));
                }else if(temp.length() > 5) {
                    mIb_delete.setVisibility(View.VISIBLE);
                    mBt_next.setEnabled(true);
                    mBt_next.setBackgroundDrawable(getResources().getDrawable(R.drawable.backups_button_selector));
                }else {
                    mIb_delete.setVisibility(View.VISIBLE);
                    mBt_next.setEnabled(false);
                    mBt_next.setBackgroundDrawable(getResources().getDrawable(R.drawable.backups_button_normal));
                }
            }
        });
        mBt_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //布局获取新密码
               datas2 =  mEt_password.getText().toString();
                String encode = Base58.encode(datas2.getBytes());
                log.e("aaa",datas);
                log.e("aaa",encode);
                if(encode.equals(datas)){
                    startActivity(new Intent(getApplicationContext(),ModifyPasswordSecondActivity.class));
                    finish();
                    //下一页
                    overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);

                }else{
                    Toast.makeText(getApplicationContext(),R.string.Password_error,Toast.LENGTH_SHORT).show();
                }

            }
        });
        mIb_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEt_password.setText("");
            }
        });
        mIb_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHideFirst == true) {
                    mIb_look.setImageResource(R.drawable.icon_xianshixinxi);
                    //密文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    mEt_password.setTransformationMethod(method1);
                    isHideFirst = false;
                } else {
                    mIb_look.setImageResource(R.drawable.icon_buxianshixinxi);
                    //密文
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    mEt_password.setTransformationMethod(method);
                    isHideFirst = true;

                }
                // 光标的位置  
                int index = mEt_password.getText().toString().length();
                mEt_password.setSelection(index);
            }
        });
        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onBackPressed();
            }
        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(getApplicationContext(),SettingActivity.class));
        finish();

        //上一页
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);

    }

    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
